package com.ruoyi.biz.letter.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.biz.attchment.domain.BizAttachment;
import com.ruoyi.biz.attchment.service.IBizAttachmentService;
import com.ruoyi.biz.letter.domain.BizMayorHotline;
import com.ruoyi.biz.letter.export.BizMayorHotlineExport;
import com.ruoyi.biz.letter.export.BizRecordLetterExport;
import com.ruoyi.biz.letter.util.AcceptDeptUtil;
import com.ruoyi.biz.letter.util.CategoryUtil;
import com.ruoyi.common.enums.AttachementType;
import com.ruoyi.common.enums.ValidType;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.biz.letter.domain.BizRecordLetter;
import com.ruoyi.biz.letter.service.IBizRecordLetterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 督办事项登记Controller
 *
 * @author Fei.Cong
 * @date 2024-07-11
 */
@RestController
@RequestMapping("/record/letter")
public class BizRecordLetterController extends BaseController {

    @Autowired
    private IBizRecordLetterService bizRecordLetterService;

    @Autowired
    private IBizAttachmentService bizAttachmentService;

    /**
     * 查询督办事项登记列表
     */
    @PreAuthorize("@ss.hasPermi('record:letter:list')")
    @GetMapping("/list")
    public AjaxResult list(BizRecordLetter bizRecordLetter) {
        LambdaQueryWrapper<BizRecordLetter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return success(bizRecordLetterService.list(lambdaQueryWrapper));
    }

    /**
     * 分页查询督办事项登记列表
     */
    @PreAuthorize("@ss.hasPermi('record:letter:list')")
    @GetMapping("/page")
    public TableDataInfo page(BizRecordLetter bizRecordLetter) {
        CategoryUtil.handleQueryCategoryId(bizRecordLetter);
        AcceptDeptUtil.handleQueryAcceptDept(bizRecordLetter);
        return getDataTable(bizRecordLetterService.pageList(bizRecordLetter, startPage()));
    }

    /**
     * 导出督办事项登记列表
     */
    @PreAuthorize("@ss.hasPermi('record:letter:export')")
    @Log(title = "督办事项登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizRecordLetter bizRecordLetter) {
        List<BizRecordLetter> list = bizRecordLetterService.queryList(bizRecordLetter);
        List<BizRecordLetterExport> exportList = list.stream().map(v -> {
            BizRecordLetterExport export = new BizRecordLetterExport();
            BeanUtils.copyProperties(v, export);
            export.setFirstCategoryName(Optional.ofNullable(v.getPCategoryId()).map(val -> String.valueOf(val)).orElse(null));
            export.setSecondCategoryName(Optional.ofNullable(v.getSCategoryId()).map(val -> String.valueOf(val)).orElse(null));
            return export;
        }).collect(Collectors.toList());
        ExcelUtil<BizRecordLetterExport> util = new ExcelUtil<BizRecordLetterExport>(BizRecordLetterExport.class);
        util.exportEasyExcel(response, exportList, "事项登记数据");
    }

    /**
     * 获取督办事项登记详细信息
     */
    @PreAuthorize("@ss.hasPermi('record:letter:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        BizRecordLetter signature = bizRecordLetterService.getById(id);
        CategoryUtil.getCategoryIds(signature);
        AcceptDeptUtil.getAcceptDept(signature);
        List<BizAttachment> attachments = bizAttachmentService.list(new LambdaQueryWrapper<BizAttachment>()
                .eq(BizAttachment::getApplyId, id)
                .eq(BizAttachment::getAttachmentType, AttachementType.LETTER_ATTACHEMENT.getCode())
                .eq(BizAttachment::getValid, ValidType.VALID.getCode()));
        if (CollectionUtil.isNotEmpty(attachments)) {
            signature.setAttachments(attachments.stream().map(BizAttachment::getFilePath).collect(Collectors.joining(",")));
        }
        List<BizAttachment> attachments1 = bizAttachmentService.list(new LambdaQueryWrapper<BizAttachment>()
                .eq(BizAttachment::getApplyId, id)
                .eq(BizAttachment::getAttachmentType, AttachementType.LETTER_ORIGIN_ATTACHEMENT.getCode())
                .eq(BizAttachment::getValid, ValidType.VALID.getCode()));
        if (CollectionUtil.isNotEmpty(attachments1)) {
            signature.setAttachments1(attachments1.stream().map(BizAttachment::getFilePath).collect(Collectors.joining(",")));
        }
        return success(signature);
    }

    /**
     * 新增督办事项登记
     */
    @PreAuthorize("@ss.hasPermi('record:letter:add')")
    @Log(title = "督办事项登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizRecordLetter bizRecordLetter) {
        bizRecordLetterService.add(bizRecordLetter);
        return success();
    }

    /**
     * 修改督办事项登记
     */
    @PreAuthorize("@ss.hasPermi('record:letter:edit')")
    @Log(title = "督办事项登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizRecordLetter bizRecordLetter) {
        bizRecordLetterService.edit(bizRecordLetter);
        return success();
    }

    /**
     * 删除督办事项登记
     */
    @PreAuthorize("@ss.hasPermi('record:letter:remove')")
    @Log(title = "督办事项登记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(bizRecordLetterService.removeBatchByIds(ids));
    }

}